package com.zhangh.summer.admin.support.prometheus.common.client;

import io.prometheus.client.Summary;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @description: TODO
 * @author: zhanghui  zhanghlucky@163.com
 * @create: 2020-08-26 15:03
 **/
@Slf4j
public class SummaryClient {
    private Map<String, Summary> SUMMARY_MAP=new HashMap<>();

    public SummaryClient register(String name,String help,String... labelNames){
        if (null==SUMMARY_MAP.get(name)) {
            Summary client = Summary.build().
                    name(name).
                    labelNames(labelNames).
                    help(help).
                    register();
            SUMMARY_MAP.put(name,client);
            log.info("Metrics SummaryClient register name:{},help:{},labelNames;{}",name,help,labelNames);
        }else{
            log.warn("Metrics SummaryClient exist name:{}",name);
        }
        return this;
    }
    public SummaryClient register(String name, Summary client){
        if (null==SUMMARY_MAP.get(name)) {
            client.register();
            SUMMARY_MAP.put(name,client);
            log.info("Metrics SummaryClient register name:{}",name);
        }else{
            log.warn("Metrics SummaryClient exist name:{}",name);
        }
        return this;
    }
    public Summary get(String name){
        Summary client=SUMMARY_MAP.get(name);
        if (null!=client){
            return client;
        }
        log.error("can not find counter:{} ,please register it",name);
        return null;
    }
}
